__author__ = 'liuyi'

import os
import sys

import getopt
import codecs
import binarize
import cPickle

from operator import itemgetter

sys.setdefaultencoding('utf-8')


if __name__ == '__main__':
    import sys
    import config

    try:
        opts, args = getopt.getopt(sys.argv[1:], "i:c:t:")
    except getopt.GetoptError:
        print "\nUsage: train_example.py [-i inputfile] [-c config] [-t type]\n"
        sys.exit(1)

    inFile = None
    configFile = None
    file_type = ''

    for opt in opts:
        if opt[0] == '-i':
            inFile = opt[1]
        if opt[0] == '-c':
            configFile = opt[1]
        if opt[0] == '-t':
            file_type = opt[1]
            

    if inFile == None or configFile == None or file_type == '':
        print "\nUsage: train_example.py [-i inputfile] [-c config] [-t type]\n"
        sys.exit(1)

    file = codecs.open(inFile, encoding='utf-8')
    configSet = config.CConfig(configFile)

    ops = []
    corpus = []
    word = []
    pos = []
    ntermset = set()
    opres = []

    for line in file:
        znode = binarize.CBinarizedTreeNode()
        znode.load(line)
        optmp = []
        wordtmp = []
        postmp = []
        znode.train_example(optmp, wordtmp, postmp, ntermset)
        corpus.append(wordtmp)
        word.extend(wordtmp)
        ops.append(optmp)
        pos.append(postmp)


    with open(configSet.post_dir + 'nterm.' + file_type, 'wb') as f:
        for nterm in ntermset:
            f.write(nterm)
            f.write('\n')

    with open(configSet.post_dir + 'operator.' + file_type, 'wb') as f:
        for sent in ops:
            for i in range(0, len(sent)):
                f.write(sent[i])
                f.write(' ') 
            f.write('\n')

    with open(configSet.post_dir + 'word.' + file_type, 'w') as f:
        for sent in corpus:
            for w in sent:
                f.write(w)
                f.write(' ') 
            f.write('\n')

    with open(configSet.post_dir + 'pos.' + file_type, 'w') as f:
        for sent in pos:
            for w in sent:
                f.write(w)
                f.write(' ') 
            f.write('\n')
